package fr.obsmip.sedoo.client.ui.metadata.experimentalsite;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Widget;

import fr.obsmip.sedoo.client.message.Message;
import fr.obsmip.sedoo.client.ui.ExperimentalSiteEntryView;
import fr.obsmip.sedoo.client.ui.breadcrumb.MetadataBreadcrumb;
import fr.obsmip.sedoo.client.ui.metadata.common.ContactTab;
import fr.obsmip.sedoo.client.ui.metadata.common.children.ConstraintTab;
import fr.obsmip.sedoo.client.ui.metadata.common.serie.OtherTab;
import fr.obsmip.sedoo.client.ui.metadata.common.serie.TemporalExtendTab;
import fr.obsmip.sedoo.shared.domain.ExperimentalSiteDTO;
import fr.sedoo.commons.client.util.ElementUtil;
import fr.sedoo.commons.client.util.StringUtil;
import fr.sedoo.commons.client.widget.map.impl.AreaSelectorWidget;
import fr.sedoo.commons.shared.domain.AbstractDTO;
import fr.sedoo.metadata.client.message.MetadataMessage;
import fr.sedoo.metadata.client.ui.view.AbstractDTOEditingView;
import fr.sedoo.metadata.client.ui.view.MetadataDisplayingView;
import fr.sedoo.metadata.client.ui.view.common.AbstractTab;
import fr.sedoo.metadata.client.ui.view.common.MetadataTabPanel;
import fr.sedoo.metadata.client.ui.view.presenter.DisplayPresenter;
import fr.sedoo.metadata.client.ui.view.presenter.EditingPresenter;
import fr.sedoo.metadata.shared.domain.MetadataDTO;
import fr.sedoo.metadata.shared.domain.MetadataSummaryDTO;
import fr.sedoo.metadata.shared.domain.dto.I18nString;

public class ExperimentalSiteMetadataViewImpl extends AbstractDTOEditingView implements ExperimentalSiteEntryView, MetadataDisplayingView {

	private static MetadataEditingViewImplUiBinder uiBinder = GWT.create(MetadataEditingViewImplUiBinder.class);

	private ExperimentalSiteEntryPresenter presenter;

	ArrayList<AbstractTab> tabs = new ArrayList<AbstractTab>();

	interface MetadataEditingViewImplUiBinder extends UiBinder<Widget, ExperimentalSiteMetadataViewImpl> {
	}


	@UiField
	Button backButton;

	@UiField
	Button printButton;
	
	@UiField
	Button xmlButton;
	
	@UiField
	Button saveButton;

	@UiField
	Button validateButton;
	
	@UiField
	MetadataBreadcrumb breadcrumb;

	@UiField(provided = true)
	MetadataTabPanel tabPanel;
	
	private String uuid;

	private String hierarchyLevel;

	private String hierarchyLevelName;

	private String metadataLastModificationDate;
	
	private MetadataSummaryDTO parentSummary;

	private DatasetsTab datasetsTab;

	private ConstraintTab constraintTab;

	private ContactTab contactTab;

	public ExperimentalSiteMetadataViewImpl(ArrayList<String> displayLanguages, List<String> dataLanguages) {
		super();
		tabPanel = new MetadataTabPanel();

		tabPanel.addTab(new IdentificationTab(displayLanguages), MetadataMessage.INSTANCE.metadataEditingIdentificationTabHeader());
		contactTab = new ContactTab();
		tabPanel.addTab(contactTab, MetadataMessage.INSTANCE.metadataEditingContactTabHeader());
		//tabPanel.addTab(new KeywordTab(currentLanguage), MetadataMessage.INSTANCE.metadataEditingKeywordTabHeader());
		tabPanel.addTab(new SingleGeographicalLocationTab(AreaSelectorWidget.GOOGLE_LAYER), MetadataMessage.INSTANCE.metadataEditingGeographicalLocationTabHeader());
		tabPanel.addTab(new TemporalExtendTab(), MetadataMessage.INSTANCE.metadataEditingTemporalExtentHeader());
		constraintTab = new ConstraintTab(displayLanguages);
		tabPanel.addTab(constraintTab, MetadataMessage.INSTANCE.metadataEditingConstraintTabHeader());
		tabPanel.addTab(new OtherTab(displayLanguages, dataLanguages), MetadataMessage.INSTANCE.metadataEditingOtherTabHeader());
		datasetsTab = new DatasetsTab();
		tabPanel.addTab(datasetsTab, Message.INSTANCE.dataSets());
		tabPanel.activateSelectionHandler();
		initWidget(uiBinder.createAndBindUi(this));
		applyCommonStyle();
		reset();

	}

	@Override
	public void reset() {
		breadcrumb.reset();
		breadcrumb.setLevel(MetadataBreadcrumb.EXPERIMENTAL_SITE_LEVEL);
		tabPanel.reset();
		ElementUtil.hide(xmlButton);
		ElementUtil.hide(printButton);
		ElementUtil.hide(saveButton);
		ElementUtil.hide(validateButton);
		uuid="";
	}

	@Override
	public void edit(AbstractDTO dto) {
		reset();
		ExperimentalSiteDTO metadata = (ExperimentalSiteDTO) dto;
		breadcrumb.enableEditMode();
		breadcrumb.setObservatoryName(metadata.getParentSummary().getName());
		breadcrumb.setObservatoryUuid(metadata.getParentSummary().getUuid());
		breadcrumb.setExperimentalSiteName(metadata.getExperimentalSiteName());
		tabPanel.edit(metadata);
		ElementUtil.hide(xmlButton);
		ElementUtil.hide(printButton);
		ElementUtil.show(saveButton);
		ElementUtil.show(validateButton);
		uuid = StringUtil.trimToEmpty(metadata.getOtherPart().getUuid());
		parentSummary = metadata.getParentSummary();
		hierarchyLevel = metadata.getIdentificationPart().getHierarchyLevel();
		hierarchyLevelName = metadata.getIdentificationPart().getHierarchyLevelName();
		metadataLastModificationDate = StringUtil.trimToEmpty(metadata.getOtherPart().getMetadataLastModificationDate());
	}

	@Override
	public void display(AbstractDTO dto) {
		reset();
		ExperimentalSiteDTO metadata = (ExperimentalSiteDTO) dto;
		breadcrumb.enableDisplayMode();
		breadcrumb.setObservatoryName(metadata.getParentSummary().getName());
		breadcrumb.setObservatoryUuid(metadata.getParentSummary().getUuid());
		breadcrumb.setExperimentalSiteName(metadata.getExperimentalSiteName());
		tabPanel.display(metadata);
		uuid = StringUtil.trimToEmpty(metadata.getOtherPart().getUuid());
		ElementUtil.show(xmlButton);
		ElementUtil.show(printButton);
		ElementUtil.hide(saveButton);
		ElementUtil.hide(validateButton);
	}

	@Override
	public MetadataDTO flush() {
		ExperimentalSiteDTO metadataDTO = new ExperimentalSiteDTO();
		tabPanel.flush(metadataDTO);
		metadataDTO.getIdentificationPart().setHierarchyLevel(hierarchyLevel);
		metadataDTO.getIdentificationPart().setHierarchyLevelName(hierarchyLevelName);
		metadataDTO.getOtherPart().setUuid(uuid);
		metadataDTO.setParentSummary(parentSummary);
		metadataDTO.getOtherPart().setMetadataLastModificationDate(metadataLastModificationDate);
		return metadataDTO;
	}

	@UiHandler("validateButton")
	void onValidateButtonClicked(ClickEvent event) {
		MetadataDTO dto = flush();
		presenter.validate(dto);
	}
	
	@UiHandler("printButton")
	void onPrintButtonClicked(ClickEvent event) {
		if (StringUtil.isNotEmpty(uuid))
		{
			presenter.print(uuid);
		}
	}
	
	@UiHandler("xmlButton")
	void onXmlButtonClicked(ClickEvent event) {
		if (StringUtil.isNotEmpty(uuid))
		{
			presenter.xml(uuid);
		}
	}
	
	@UiHandler("backButton")
	void onBackButtonClicked(ClickEvent event) {
		if (presenter != null)
		{
			presenter.back();
		}
		else if (presenter != null)
		{
			presenter.back();
		}
	}

	

	@UiHandler("saveButton")
	void onSaveButtonClicked(ClickEvent event) {
		MetadataDTO dto = flush();
		presenter.save(dto);
	}

	@Override
	public void setEditingPresenter(EditingPresenter editingPresenter) {
		//Nothing is done via this way
	}

	@Override
	public void setDisplayPresenter(DisplayPresenter displayPresenter) {
		//Nothing is done via this way
	}
	
	@Override
	public void setPresenter(ExperimentalSiteEntryPresenter presenter) {
		this.presenter = presenter;
		datasetsTab.setPresenter(presenter);
		constraintTab.setPresenter(presenter);
		contactTab.setPresenter(presenter);
	}

	@Override
	public void broadcastExperimentalSiteDeletion(String uuid) {
		datasetsTab.broadcastDatasetDeletion(uuid);
	}

	@Override
	public void setUseConditions(I18nString result) {
		constraintTab.setUseConditions(result);
	}

	@Override
	public void setPublicAccessLimitations(I18nString result) {
		constraintTab.setPublicAccessLimitations(result);
	}

	
	

	

	

}
